package com.pangolin.pangolinim.ui.activity;

import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.lqr.recyclerview.LQRRecyclerView;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.service.list.ContactList;
import com.pangolin.pangolinim.sqlite.DBContact;
import com.pangolin.pangolinim.ui.adapter.NewFriendAdapter;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.ImageUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewFriendActivity extends BaseActivity {
    public LQRRecyclerView mRvNewFriend;
    private NewFriendAdapter mAdapter;
    public List<UIContact> mContacts;
    private EditText mEtSearch;

    private UIContact me;

    @Override
    protected void init(){
        mContacts = new ArrayList<UIContact>();

        String strContact = getIntent().getStringExtra("param1");
        if(strContact != null){
            me = GsonUtil.fromJson(strContact , UIContact.class);
            Logger.d("contact:%d|%s" , me.getUid() , me.getName());
        }
    }

    @Override
    protected void initView(){
        setToolbarTitle(UIUtil.getString(R.string.new_friend));
        mRvNewFriend = findViewById(R.id.rvNewFriend);
        mEtSearch = findViewById(R.id.etSearch);
        setAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注册广播接收器
        Map<String, Object> brocastMap = new HashMap<String, Object>();
        brocastMap.put("type" , BroadcastMsg.DATA_UI_NEWFRIEND);
        BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
        BroadcastSender(bundleUtil.getBundle());
    }

    @Override
    protected void initListener(){
        mEtSearch.setOnClickListener(v -> jumpToActivity(SearchUserActivity.class));
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA){
            if(bundleUtil.getData() != null){
                Map<String, Object> resMap = bundleUtil.getData();
                Logger.d(resMap);
                if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_NEWFRIEND){
                    if((int)resMap.get("result") == 0){
                        loadNewFriends((String) resMap.get("newfriends"));
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }else if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_CONFIRM){
                    if((int)resMap.get("result") == 0){
                        UIUtil.showToast("操作成功");
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }
            }
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_new_friend;
    }

    private void setAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRvNewFriend.setLayoutManager(layoutManager);
        mAdapter = new NewFriendAdapter(mContacts);
        mRvNewFriend.setAdapter(mAdapter);
    }

    /**
     * TODO:反序列化新的朋友列表数据并加载到UI
     * @param str_newfriends 广播数据中序列化成字符串的ContactList类
     */
    private void loadNewFriends(String str_newfriends){
        //反序列化
        ContactList contactList = GsonUtil.fromJson(str_newfriends , ContactList.class);
        if(contactList != null){
            List<DBContact> dbContacts = contactList.get_allcontacts();
            for (DBContact dbContact : dbContacts){
                UIContact uiContact = new UIContact();
                uiContact.setUid(dbContact.getUid());
                uiContact.setName(dbContact.getName());
                uiContact.setHeader(dbContact.getHeadimg());
                uiContact.setType(dbContact.getType());
                File file = new File(getExternalFilesDir(String.valueOf(me.getUid())) , uiContact.getHeader());
                if(file.exists() && file.isFile()){
                    uiContact.setBitHeader(ImageUtil.getBitmapFromFile(this , file , 120 , 120));
                }
                mContacts.add(uiContact);
            }
            mAdapter.notifyDataSetChanged();
        }
    }
}